package com.chunuo.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chunuo.aclservice.entity.User;
import com.chunuo.aclservice.service.UserRoleService;
import com.chunuo.aclservice.service.UserService;
import com.chunuo.commonutils.MD5;
import com.chunuo.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/aclservice/user")
public class UserController {

    //分页获取、获取一个、删除一个、批量删除、更新、保存、用户绑定角色、根据用户id获取角色信息
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("分页获取用户信息")
    @GetMapping("getPageList/{page}/{limit}")
    public R pageList(@PathVariable("page")Long page,
                      @PathVariable("limit")Long limit,
                      User searchObj){
        IPage pageList = new Page(page,limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(searchObj.getUsername())){
            wrapper.like("username",searchObj.getUsername());
        }
        IPage userPageList = userService.page(pageList,wrapper);
        // 数据和总条数
        return R.ok().data("items", userPageList.getRecords()).data("total", userPageList.getTotal());
    }


    @ApiOperation("获取用户信息")
    @GetMapping("info/{userId}")
    public R getInfo(@ApiParam("用户Id") @PathVariable("userId")String userId){
        User user = userService.getById(userId);
        return R.ok().data("info",user);
    }

    @ApiOperation("删除一个用户")
    @DeleteMapping("delete/{userId}")
    public R deleteById(@ApiParam("用户Id") @PathVariable("userId")String userId){
        userService.removeById(userId);
        return R.ok();
    }

    @ApiOperation("根据id批量删除用户")
    @DeleteMapping("delteBatch")
    public R deleteBatch(@ApiParam("List集合的Id") @RequestBody List<String> ids){
        userService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation("根据Id更新")
    @PutMapping("updateInfo")
    public R updateById(@ApiParam("根据id更新用户信息") @RequestBody User user){
        userService.updateById(user);
        return R.ok();
    }

    @ApiOperation("添加用户")
    @PostMapping("save")
    public R save(@ApiParam("保存的用户信息") @RequestBody User user){
        //对密码进行加密
        user.setPassword(MD5.encrypt(user.getPassword()));
        userService.save(user);
        return R.ok();
    }

    @ApiOperation("用户绑定角色")
    @PostMapping("userBindRole/{userId}")
    public R userBindRole(@PathVariable("userId")String userId,
                          @RequestBody List<String> roleIds){
        userRoleService.batchBind(userId,roleIds);
        return R.ok();
    }

    @ApiOperation("用户获取角色信息")
    @GetMapping("getUserBindRole/{userId}")
    public R getUserBindRole(@PathVariable("userId")String userId){
        Map map = userRoleService.getUserBindRole(userId);
        return R.ok().data(map);
    }

}

